//
//  kWGoodCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/5/24.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "kWGoodCell.h"
#import "kWHomeModel.h"

@interface kWGoodCell()

@property(nonatomic,strong)UIImageView        *contentImg;
@property(nonatomic,strong)UILabel            *contentLabel;
@property(nonatomic,strong)UILabel            *contentPrice;
@property(nonatomic,strong)UILabel            *detailLabel;
@property(nonatomic,strong)UIButton           *buyBtn;

@end

@implementation kWGoodCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buildSubViews];
    }
    return self;
}

- (void)buildSubViews
{
    [self.contentView addSubview:self.contentImg];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.detailLabel];
    [self.contentView addSubview:self.contentPrice];
    [self.contentView addSubview:self.buyBtn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat viewW = self.width;
    CGFloat viewH = self.Height;
    CGFloat margin = 15;
    
    
    
    WeakSelf(self);
    [self.contentImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakself.contentView);
        make.top.equalTo(weakself.contentView.mas_top);
        make.width.mas_equalTo(viewW-margin*2);
        make.height.mas_equalTo(viewH * 0.7);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakself.contentImg);
        make.top.equalTo(weakself.contentImg.mas_bottom).offset(5);
        make.width.equalTo(weakself.contentImg);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakself.contentImg);
        make.width.equalTo(weakself.contentImg);
        make.top.equalTo(weakself.contentLabel.mas_bottom).offset(5);
    }];
    
    [self.contentPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakself.contentImg);
        make.top.equalTo(weakself.detailLabel.mas_bottom).offset(10);
    }];
    
    [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakself.contentImg);
        make.centerY.equalTo(weakself.contentPrice);
        make.width.mas_equalTo(55);
        make.height.mas_equalTo(20);
    }];
    
}

- (void)setHomeModel:(kWHomeModel *)homeModel
{
    _homeModel = homeModel;
    
    NSURL *url = [NSURL URLWithString:homeModel.img];
    [self.contentImg sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.contentLabel.text = homeModel.title;
    self.detailLabel.text = homeModel.about;
    self.contentPrice.text = [NSString stringWithFormat:@"¥%@",homeModel.price];
}

- (void)likeClick
{
    if (self.likeBlock) {
        self.likeBlock();
    }
}


#pragma mark lazy
- (UIImageView *)contentImg
{
    if (!_contentImg) {
        _contentImg = [[UIImageView alloc] init];
        _contentImg.image = [UIImage imageNamed:@"kW_3d_bg"];
    }return _contentImg;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = FONT(14);
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.text = @"凌波阁欧式橱柜";
    }
    return _contentLabel;
}

- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = FONT(11);
        _detailLabel.textColor = [UIColor lightGrayColor];
        _detailLabel.text = @"arch chairsyToTesco2.0[1033:54903] 1[]__tcp_connection_write_eof_block_invoke Write close callback received error: [22] Invalid argument";
    }
    return _detailLabel;
}

- (UIButton *)buyBtn
{
    if (!_buyBtn) {
        _buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buyBtn setTitle:@"购买" forState:UIControlStateNormal];
        _buyBtn.titleLabel.font = FONT(12);
        [_buyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _buyBtn.layer.borderWidth = 1;
        _buyBtn.layer.masksToBounds = true;
        _buyBtn.layer.cornerRadius = 3;
        
        [_buyBtn addTarget:self action:@selector(likeClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buyBtn;
}


- (UILabel *)contentPrice
{
    if (!_contentPrice) {
        _contentPrice = [[UILabel alloc] init];
        _contentPrice.textColor = UIColorFromRGB(0xfa3802);
        _contentPrice.font = FONT(14);
        _contentPrice.textAlignment = NSTextAlignmentRight;
        _contentPrice.text = @"¥67";
    }
    return _contentPrice;
}


@end
